package priv.lirenhe.javasuper.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPReceiverTest {//非面向连接的协议，不安全

	public UDPReceiverTest() {
		
	}

	public static void main(String[] args) {
		try {
			DatagramSocket receiver = new DatagramSocket(8094);//dos命令下如何查看8094端口是否被占用
			
			byte[] buf = new byte[1024];//65535    0~1024      1025~65535
			
			DatagramPacket dataP = new DatagramPacket(buf,buf.length);
			System.out.println("hold on");
			receiver.receive(dataP);//线程阻塞 sleep join io
			String data = new String(dataP.getData(), 0, buf.length);
			System.out.println("data:" + data);
			System.out.println(dataP.getAddress().getHostAddress() + ":" + dataP.getPort());
			
			receiver.close();
			
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
}
